WORK_DIR=$(cd $(dirname $0); pwd)

# 获取用户输入 -t server -v 9.3.0 -a amd64 -p x86-64
while getopts ":a:t:v:u:p:" opt
do
    case $opt in
    	a)
        ARCH=$OPTARG;;
      t)
        DEV_TAG=$OPTARG;;
      v)
        BUILD_VERSION=$OPTARG;;
      u)
        RELEASE_URL=$OPTARG;;
      p)
        PLATFORM=$OPTARG;;
      ?)
        echo "Unknown parameter"
        exit 1;;
    esac
done

# 初始化环境变量
DEV_TAG=${DEV_TAG:-"server"}
BUILD_VERSION=${BUILD_VERSION:-"9.3.0"}
RELEASE_URL=${RELEASE_URL:-"https://docker.lan.yunnan.ws/downloads/onedev-${BUILD_VERSION}.zip"}
ARCH=${ARCH:-"amd64"}
PLATFORM=${PLATFORM:-"x86-64"}

cd ${WORK_DIR}/${DEV_TAG}
# 构建容器
docker build -f ${DEV_TAG}.Dockerfile \
	-t tekintian/onedev-${DEV_TAG}:${BUILD_VERSION}  \
	--build-arg ARCH=${ARCH} \
  --build-arg PLATFORM=${PLATFORM} \
  --build-arg BUILD_VERSION=${BUILD_VERSION} \
	--build-arg RELEASE_URL=${RELEASE_URL} \
	.


docker tag tekintian/onedev-${DEV_TAG}:${BUILD_VERSION} tekintian/onedev-${DEV_TAG}
docker push tekintian/onedev-${DEV_TAG}:${BUILD_VERSION}
docker push tekintian/onedev-${DEV_TAG}

